package ar.uba.dc.cmovil.tripplanner.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by gbarbuto on 9/24/13.
 */
public class Trip implements Serializable {

    private UUID id;
    private String name;
    private Date startDate;
    private ArrayList<Place> places;

    public Trip(String name, Date startDate) {
        id = UUID.randomUUID();
        this.setName(name);
        this.setStartDate(startDate);
        this.places = new ArrayList<Place>();
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        if (name == null) {
            throw new IllegalArgumentException("name cannot be null");
        }

        String trimmedName = name.trim();

        if (trimmedName.isEmpty()) {
            throw new IllegalArgumentException("name cannot be empty");
        }

        this.name = trimmedName;
    }

    public Date getStartDate() {
        return this.startDate;
    }

    public void setStartDate(Date startDate) {
        if (startDate == null) {
            throw new IllegalArgumentException("startDate cannot be null");
        }

        this.startDate = startDate;
    }

    public List<Place> getPlaces() {
        return new ArrayList<Place>(this.places);
    }

    public void add(Place place) {
        this.add(this.places.size(), place);
    }

    public void add(int index, Place place) {
        if (index < 0 || index > this.places.size()) {
            throw new IndexOutOfBoundsException("index");
        }

        if (place == null) {
            throw new IllegalArgumentException("place cannot be null");
        }

        this.places.add(index, place);
    }

    public Place remove(int index) {
        if (index < 0 || index >= this.places.size()) {
            throw new IndexOutOfBoundsException();
        }

        return this.places.remove(index);
    }

    public void remove(Place place) {
        if (place == null) {
            throw new IllegalArgumentException("place cannot be null");
        }

        this.places.remove(place);
    }

    public UUID getId() {
        return id;
    }
}
